<template>
  <nav class="navbar">
    <div class="navbar-container">
      <div class="navbar-brand">
        <router-link to="/" class="brand-link">
          <span class="brand-icon">🎨</span>
          <span class="brand-text">AI 3D Generator</span>
        </router-link>
      </div>
      
      <div class="navbar-menu">
        <div v-if="isLoggedIn" class="user-menu">
          <div class="user-info">
            <span class="user-avatar">👤</span>
            <span class="username">{{ currentUser?.username || '用户' }}</span>
          </div>
          <div class="menu-dropdown">
            <button @click="toggleDropdown" class="dropdown-toggle">
              <span>▼</span>
            </button>
            <div v-if="showDropdown" class="dropdown-menu">
              <router-link to="/profile" class="dropdown-item">
                <span>👤</span>
                个人资料
              </router-link>
              <router-link to="/history" class="dropdown-item">
                <span>📝</span>
                生成历史
              </router-link>
              <button @click="handleLogout" class="dropdown-item logout-item">
                <span>🚪</span>
                退出登录
              </button>
            </div>
          </div>
        </div>
        
        <div v-else class="auth-buttons">
          <router-link to="/login" class="auth-button login-button">
            登录
          </router-link>
          <router-link to="/register" class="auth-button register-button">
            注册
          </router-link>
        </div>
      </div>
    </div>
  </nav>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'NavBar',
  data() {
    return {
      showDropdown: false
    }
  },
  computed: {
    ...mapGetters(['isLoggedIn', 'currentUser'])
  },
  methods: {
    ...mapActions(['logout']),
    
    toggleDropdown() {
      this.showDropdown = !this.showDropdown
    },
    
    async handleLogout() {
      this.showDropdown = false
      await this.logout()
      this.$router.push('/login')
    }
  },
  mounted() {
    // 点击外部关闭下拉菜单
    document.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.showDropdown = false
      }
    })
  }
}
</script>

<style scoped>
.navbar {
  background: white;
  border-bottom: 1px solid #e1e5e9;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.navbar-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.navbar-brand {
  display: flex;
  align-items: center;
}

.brand-link {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: #333;
  font-weight: 600;
  font-size: 18px;
  transition: color 0.3s ease;
}

.brand-link:hover {
  color: #667eea;
}

.brand-icon {
  font-size: 24px;
}

.brand-text {
  font-size: 18px;
}

.navbar-menu {
  display: flex;
  align-items: center;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: #f8f9fa;
  border: 1px solid #e1e5e9;
}

.user-avatar {
  font-size: 16px;
}

.username {
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.menu-dropdown {
  position: relative;
}

.dropdown-toggle {
  background: none;
  border: none;
  padding: 8px;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.3s ease;
  color: #666;
  font-size: 12px;
}

.dropdown-toggle:hover {
  background-color: #f8f9fa;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 160px;
  z-index: 1001;
  animation: dropdownFadeIn 0.2s ease-out;
}

@keyframes dropdownFadeIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  text-decoration: none;
  color: #333;
  font-size: 14px;
  transition: background-color 0.3s ease;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
}

.dropdown-item:hover {
  background-color: #f8f9fa;
}

.logout-item {
  color: #c53030;
}

.logout-item:hover {
  background-color: #fee;
}

.auth-buttons {
  display: flex;
  align-items: center;
  gap: 12px;
}

.auth-button {
  padding: 8px 16px;
  border-radius: 6px;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 1px solid transparent;
}

.login-button {
  color: #667eea;
  border-color: #667eea;
}

.login-button:hover {
  background-color: #667eea;
  color: white;
}

.register-button {
  background-color: #667eea;
  color: white;
}

.register-button:hover {
  background-color: #5a67d8;
  transform: translateY(-1px);
}

@media (max-width: 768px) {
  .navbar-container {
    padding: 0 16px;
  }
  
  .brand-text {
    display: none;
  }
  
  .username {
    display: none;
  }
  
  .auth-buttons {
    gap: 8px;
  }
  
  .auth-button {
    padding: 6px 12px;
    font-size: 13px;
  }
}
</style>
